<?php

/**
 * This is the model class for table "es_goods".
 *
 * The followings are the available columns in table 'es_goods':
 * @property string $goods_id
 * @property string $goods_name
 * @property string $goods_price
 * @property string $goods_discount
 * @property integer $goods_type_id
 * @property string $goods_type_name
 * @property string $add_time
 * @property string $goods_pictrues
 * @property integer $status
 * @property string $average_score
 */
class Goods extends CActiveRecord
{
	public $image;
	/**
	 * Returns the static model of the specified AR class.
	 * @param string $className active record class name.
	 * @return Goods the static model class
	 */
	public static function model($className=__CLASS__)
	{
		return parent::model($className);
	}

	/**
	 * @return string the associated database table name
	 */
	public function tableName()
	{
		return 'es_goods';
	}

	/**
	 * @return array validation rules for model attributes.
	 */
	public function rules()
	{
		// NOTE: you should only define rules for those attributes that
		// will receive user inputs.
		return array(
			array('goods_name, goods_price, goods_discount, goods_type_id', 'required'),
			array('goods_type_id, status', 'numerical', 'integerOnly'=>true),
			array('goods_price', 'length', 'max'=>10),
			array('add_time', 'length', 'max'=>45),
			array('goods_pictrues', 'length', 'max'=>255),
			// The following rule is used by search().
			// Please remove those attributes that should not be searched.
			array('goods_id, goods_name, goods_price, goods_discount, goods_type_id, goods_type_name, add_time, goods_pictrues, status, average_score', 'safe', 'on'=>'search'),
		);
	}

	/**
	 * @return array relational rules.
	 */
	public function relations()
	{
		// NOTE: you may need to adjust the relation name and the related
		// class name for the relations automatically generated below.
		 return array(
			'goodsType'=> array(self::BELONGS_TO, 'GoodsType', 'goods_type_id'),
			'comments'=>array(self::HAS_MANY,'Comments','goods_id'),
			'records'=>array(self::HAS_MANY,'Records','goods_id'),
			'carts'=>array(self::MANY_MANY,'Cart','goods_id')
		);
	}
	
	/**
	 *get the add time before write to database
	 */
	public function afterValidate()
	{
		$this->add_time = time();
		$goodType = GoodsType::model()->findByPk($this->goods_type_id);
		$this->goods_type_name = $goodType->type_name;
		$this->status = 0;
		$this->average_score = 5;
	}

	/**
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels()
	{
		return array(
			'goods_id' => 'Goods',
			'goods_name' => 'Goods Name',
			'goods_price' => 'Goods Price',
			'goods_discount' => 'Goods Discount',
			'goods_type_id' => 'Goods Type',
			'goods_type_name' => 'Goods Type Name',
			'add_time' => 'Add Time',
			'goods_pictrues' => 'Goods Pictrues',
			'status' => 'Status',
			'average_score' => 'Average Score',
			'image'=>'Goods Image',
		);
	}

	/**
	 * Retrieves a list of models based on the current search/filter conditions.
	 * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
	 */
	public function search()
	{
		// Warning: Please modify the following code to remove attributes that
		// should not be searched.

		$criteria=new CDbCriteria;

		$criteria->compare('goods_id',$this->goods_id,true);
		$criteria->compare('goods_name',$this->goods_name,true);
		$criteria->compare('goods_price',$this->goods_price,true);
		$criteria->compare('goods_discount',$this->goods_discount,true);
		$criteria->compare('goods_type_id',$this->goods_type_id);
		$criteria->compare('goods_type_name',$this->goods_type_name,true);
		$criteria->compare('add_time',$this->add_time,true);
		$criteria->compare('goods_pictrues',$this->goods_pictrues,true);
		$criteria->compare('status',$this->status);
		$criteria->compare('average_score',$this->average_score,true);

		return new CActiveDataProvider($this, array(
			'criteria'=>$criteria,
		));
	}
	
	public function getGoodsTypeOpinions()
	{
		$goodsTypes =GoodsType::model()->findAll();
		$typesArray = CHtml::listData($goodsTypes, 'goods_type_id', 'type_name');
		return $typesArray;
	}
}